package com.zth.Controller;

import com.github.pagehelper.PageInfo;
import com.zth.Service.UserService;
import com.zth.entity.LayData;
import com.zth.entity.MyPage;
import com.zth.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class LoginController {
    @Autowired
    UserService userService;

    //注册
    @PostMapping("userinfo")
    public LayData reg(User user){
        System.out.println("注册信息为 = " + user);
        final  Boolean reg=userService.reg(user);
        final LayData layData=new LayData();
        layData.setData(reg);
        return  layData;
    }
    //登录
    @RequestMapping("loginDev")
    public LayData login(HttpSession session, User user) {
        LayData layData = new LayData();
        User login = userService.loginDev(user);
        if (login!=null) {
            System.out.println("登陆成功");
            layData.setData(true);
            session.setAttribute("user", login);
        }
        return layData;
    }


//    @GetMapping("queryUser")
//    public List<User> queryUser(User user) {
//        return userService.findAll();
//    }
    //分页
    @GetMapping("page")
    public LayData queryByPage(MyPage page) {
        LayData layData = new LayData();
        PageInfo all = userService.queryByPage(page);
        layData.setData(all);
        layData.setCode(0);
        return layData;
    }

    @PostMapping("updUserStatus")
    public LayData updUserStatus(User userinfo){
        System.out.println("状态 = " + userinfo.getStatus());
        System.out.println("id = " + userinfo.getId());
        LayData layVo = new LayData();

        Boolean status=userService.updStatus(userinfo);
        layVo.setData(status);
        return layVo;
    }

    @PostMapping("updUserinfo")
    public LayData updUserinfo(User user){
        System.out.println("状态 = " + user.getStatus());
        System.out.println("id = " + user.getId());
        LayData layVo = new LayData();

        Boolean updUserinfo=userService.updUserinfo(user);
        layVo.setData(updUserinfo);
        return layVo;
    }

    @GetMapping("queryById/{id}")
    public LayData queryById(@PathVariable("id") Integer id){
        LayData layVo = new LayData();

        final User userinfo = userService.queryById(id);
        layVo.setData(userinfo);
        return layVo;
    }


}
